//============================================================================
// Name        : factory_method.cpp
// Author      : Tony
// Version     :
// Copyright   : GPLv2
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

class Log{
public:
	Log()
	{
		cout<<"construct Log"<<endl;
	}
	virtual ~Log()
	{
		cout<<"Destroy Log"<<endl;
	};
	virtual void writeLog() = 0;
};

class FileLog : public Log
{
public:
	FileLog()
	{
		cout<<"construct FileLog"<<endl;
	}
	virtual ~FileLog()
	{
		cout<<"destroy FileLog"<<endl;
	}
	virtual void writeLog()
	{
		cout<<"write log to file"<<endl;
	}
};

class DatabaseLog : public Log
{
public:
	DatabaseLog()
	{
		cout<<"construct DatabaseLog"<<endl;
	}
	virtual ~DatabaseLog()
	{
		cout<<"destroy DatabaseLog"<<endl;
	}
	virtual void writeLog()
	{
		cout<<"write log to Database"<<endl;
	}
};


class LogFactory
{
public:
	LogFactory()
	{
		cout<<"construct LogFactory"<<endl;
	}
	virtual ~LogFactory()
	{
		cout<<"destroy LogFactory"<<endl;
	}

	virtual Log* createLog() = 0;
};

class FileLogFactory : public LogFactory
{
public:
	FileLogFactory()
	{
		cout<<"construct FileLogFactory"<<endl;
	}
	virtual ~FileLogFactory()
	{
		cout<<"destroy FileLogFactory"<<endl;
	}
	virtual Log* createLog()
	{
		return new FileLog();
	}
};

class DatabaseLogFactory : public LogFactory
{
public:
	DatabaseLogFactory()
	{
		cout<<"construct DatabaseLogFactory"<<endl;
	}
	virtual ~DatabaseLogFactory()
	{
		cout<<"destroy DatabaseLogFactory"<<endl;
	}
	virtual Log* createLog()
	{
		return new DatabaseLog();
	}
};

int main() {
	LogFactory* factory = new FileLogFactory();
	Log* log = factory->createLog();
	log->writeLog();
	delete log;
	delete factory;

	cout<<"===================="<<endl;

	factory = new DatabaseLogFactory();
	log = factory->createLog();
	log->writeLog();
	delete log;
	delete factory;

	return 0;
}
